/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormAllStatuses.java
 *
 * Created on 23.02.2012., 02.22.25
 */
package gui.forms;

import gui.forms.coordinator.FormCoordinator;
import gui.panels.PanelStatus;
import gui.panels.tableModels.TableModelStatus;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import model.Status;
import model.controller.ControllerFPUser;

/**
 *
 * @author Jelena
 */
public class FormAllStatuses extends javax.swing.JFrame implements observe.Observer{

    /** Creates new form FormAllStatuses */
    public FormAllStatuses() {        
        try {
            initComponents();
            if(ControllerFPUser.getInstance().getActiveUser()!=null){
            lblusername.setText(ControllerFPUser.getInstance().getActiveUser().getUsername());
            lblAvatar.setIcon(ControllerFPUser.getInstance().getActiveUser().getUserProfile().getAvatar().getAvatarImg());
            }
            tblstatus.setModel(new TableModelStatus(formCoordinator.getAllStatuses()));
            refresh();
        } catch (Exception ex) {
            Logger.getLogger(PanelStatus.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    FormCoordinator formCoordinator=  FormCoordinator.getInstance();
    private void refresh() {
        try {
            tblstatus.setModel(new TableModelStatus(formCoordinator.getAllStatuses()));
            tastatus.setText(null);
            setcbModel();
        } catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(panelNewPost, ex.getMessage());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(panelNewPost, ex.getMessage());
        }
    }
    
    private void setcbModel(){
        cbAreaOfInterest.removeAllItems();
        List<String> areasOfInterest= FormCoordinator.getInstance().getAreasOfInterest();
        for (String string : areasOfInterest) {
          cbAreaOfInterest.addItem(string);   
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        panelNewPost = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tastatus = new javax.swing.JTextArea();
        btnPost = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        lblusername = new javax.swing.JLabel();
        cbAreaOfInterest = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        lblAvatar = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblstatus = new javax.swing.JTable();
        btnFacePalm = new javax.swing.JButton();
        btnLike = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        panelNewPost.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        tastatus.setColumns(20);
        tastatus.setLineWrap(true);
        tastatus.setRows(5);
        jScrollPane1.setViewportView(tastatus);

        btnPost.setText("Postuj");
        btnPost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPostActionPerformed(evt);
            }
        });

        btnRefresh.setText("Osveži");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        lblusername.setFont(new java.awt.Font("Tahoma", 0, 24));
        lblusername.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblusername.setBorder(new javax.swing.border.MatteBorder(null));

        jLabel1.setText("Izaberi oblast u kojoj zeliš da ti se prikaze post:");

        lblAvatar.setText("labelAvatar");

        javax.swing.GroupLayout panelNewPostLayout = new javax.swing.GroupLayout(panelNewPost);
        panelNewPost.setLayout(panelNewPostLayout);
        panelNewPostLayout.setHorizontalGroup(
            panelNewPostLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelNewPostLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelNewPostLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblusername, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(panelNewPostLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAvatar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panelNewPostLayout.createSequentialGroup()
                        .addComponent(btnPost, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cbAreaOfInterest, 0, 226, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        panelNewPostLayout.setVerticalGroup(
            panelNewPostLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelNewPostLayout.createSequentialGroup()
                .addGroup(panelNewPostLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelNewPostLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblAvatar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cbAreaOfInterest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addGroup(panelNewPostLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnRefresh)
                            .addComponent(btnPost)))
                    .addGroup(panelNewPostLayout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addComponent(lblusername, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        tblstatus.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "korisničko ime", "status", "oblast", "brojLajkova", "brojFacePalmova"
            }
        ));
        jScrollPane2.setViewportView(tblstatus);

        btnFacePalm.setText("facepalmuj :D");
        btnFacePalm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFacePalmActionPerformed(evt);
            }
        });

        btnLike.setText("lajkuj :)");
        btnLike.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLikeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelNewPost, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(368, Short.MAX_VALUE)
                .addComponent(btnLike)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnFacePalm)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(panelNewPost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnFacePalm)
                    .addComponent(btnLike))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
    
}//GEN-LAST:event_formWindowOpened

private void btnPostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPostActionPerformed
        try {
            String statusText = tastatus.getText().trim();
            String areaOfInterest = String.valueOf(cbAreaOfInterest.getSelectedItem());
            
            FormCoordinator.getInstance().saveNewStatus(statusText, areaOfInterest);
            refresh();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(panelNewPost, ex.getMessage());
        } 
        
}//GEN-LAST:event_btnPostActionPerformed

private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
            refresh();
        
        
}//GEN-LAST:event_btnRefreshActionPerformed

private void btnFacePalmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFacePalmActionPerformed
        try {
            int row = tblstatus.getSelectedRow();
            TableModelStatus tms = (TableModelStatus) tblstatus.getModel();
            Status status = tms.getStatus(row);
            status.setNofacePalm(status.getNofacePalm()+1);
            FormCoordinator.getInstance().updateStatus(status);
            refresh();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FormAllStatuses.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PanelStatus.class.getName()).log(Level.SEVERE, null, ex);
        }
    
}//GEN-LAST:event_btnFacePalmActionPerformed

private void btnLikeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLikeActionPerformed
        try {
            int row = tblstatus.getSelectedRow();
            TableModelStatus tms = (TableModelStatus) tblstatus.getModel();
            Status status = tms.getStatus(row);
            status.setNoLike(status.getNoLike()+1);
            FormCoordinator.getInstance().updateStatus(status);
            refresh();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FormAllStatuses.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PanelStatus.class.getName()).log(Level.SEVERE, null, ex);
        }
    
}//GEN-LAST:event_btnLikeActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormAllStatuses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormAllStatuses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormAllStatuses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormAllStatuses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FormAllStatuses().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFacePalm;
    private javax.swing.JButton btnLike;
    private javax.swing.JButton btnPost;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JComboBox cbAreaOfInterest;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAvatar;
    private javax.swing.JLabel lblusername;
    private javax.swing.JPanel panelNewPost;
    private javax.swing.JTextArea tastatus;
    private javax.swing.JTable tblstatus;
    // End of variables declaration//GEN-END:variables

    @Override
    public void update() {
        refresh();
    }
}
